package com.suneast.suneaststore.modules.homepage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.suneast.suneaststore.API;
import com.suneast.suneaststore.bean.homepage.BrandBean;
import com.suneast.suneaststore.bean.homepage.FilterBean;
import com.suneast.suneaststore.modules.homepage.event.GotBrandList;
import com.suneast.suneaststore.modules.homepage.event.GotGoodsList;
import com.suneast.suneaststore.modules.homepage.event.GotHomePageData;
import com.suneast.suneaststore.modules.homepage.event.GotSubCate;
import com.suneast.suneaststore.modules.homepage.event.RequestResult;
import com.suneast.suneaststore.modules.common.Flag;
import com.suneast.suneaststore.bean.search.SearchBean;
import com.suneast.suneaststore.modules.common.CommonUtil;
import com.suneast.suneaststore.util.log.LatteLogger;
import com.suneast.suneaststore.util.net.RestClient;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;

public class HomePageHandler {
    static void requestHomePage() {
        RestClient
                .builder()
                .url(API.HOME_PAGE)
                .success(response -> {
                    LatteLogger.json("test", response);
                    JSONObject dataObj = CommonUtil.parseDataFromRootJSON(response);
                    if (dataObj != null) {
                        List<String> banners = JSON.parseArray(
                                dataObj.getJSONArray("banner").toJSONString()
                                , String.class);
                        List<BrandBean> brandBeans = JSON.parseArray(
                                dataObj.getJSONArray("brand").toJSONString(), BrandBean.class);
                        JSONObject goodsObj = dataObj.getJSONObject("goods");
                        int count = goodsObj.getIntValue("count");
                        int total = goodsObj.getIntValue("total");
                        List<SearchBean> searchBeans = JSON.parseArray(
                                goodsObj.getJSONArray("list").toJSONString(), SearchBean.class);
                        EventBus.getDefault().post(new GotHomePageData(
                                banners, brandBeans, searchBeans, count, total));
                    } else {
                        EventBus.getDefault().post(new RequestResult(RequestResult.HOME_PAGE, Flag.Result.FAILURE));
                    }
                })
                .error((code, msg) -> {
                    EventBus.getDefault().post(new RequestResult(RequestResult.HOME_PAGE, Flag.Result.FAILURE));
                    LatteLogger.e("error", "requestHomePage error code = "
                            + code + " msg = " + msg);
                })
                .failure(() -> {
                    EventBus.getDefault().post(new RequestResult(RequestResult.HOME_PAGE, Flag.Result.FAILURE));
                    LatteLogger.e("error", "requestHomePage failure ");
                })
                .build()
                .post();
    }

    static void requestBrandList(@Flag.Target int target, @Flag.Cid int cid) {
        RestClient
                .builder()
                .url(API.GET_BRAND)
                .params("cid", cid)
                .params("target", target)
                .success(response -> {
                    LatteLogger.d("test", "requestBrandList");
                    LatteLogger.json("test", response);
                    JSONObject rootObj = JSON.parseObject(response);
                    if (rootObj != null) {
                        if (rootObj.getIntValue("code") == 1) {
                            JSONArray dataArray = rootObj.getJSONArray("data");
                            if (dataArray != null && dataArray.size() > 0) {
                                List<BrandBean> brandBeans = JSON.parseArray(
                                        dataArray.toJSONString(), BrandBean.class);
                                EventBus.getDefault().post(new GotBrandList(brandBeans));
                            } else {
                                EventBus.getDefault().post(new GotBrandList(new ArrayList<>()));
                            }
                        } else {
                            LatteLogger.e("error", "request data success but code = "
                                    + rootObj.getIntValue("code")
                                    + " msg = " + rootObj.getString("msg"));
                        }
                    } else {
                        EventBus.getDefault().post(new RequestResult(RequestResult.BRAND_LIST, Flag.Result.FAILURE));
                    }
                })
                .error((code, msg) -> {
                    EventBus.getDefault().post(new RequestResult(RequestResult.BRAND_LIST, Flag.Result.FAILURE));
                    LatteLogger.e("error", "requestBrandList error code = "
                            + code + " msg = " + msg);
                })
                .failure(() -> {
                    EventBus.getDefault().post(new RequestResult(RequestResult.BRAND_LIST, Flag.Result.FAILURE));
                    LatteLogger.e("error", "requestBrandList failure ");
                })
                .build()
                .post();
    }

    static void requestGoodsList(WeakHashMap<String, Object> paramsMap) {
        RestClient.builder()
                .url(API.GOODS_LIST)
                .params(paramsMap)
                .success(response -> {
                            JSONObject rootJObj = JSONObject.parseObject(response);
                            LatteLogger.json("test", response);
                            if (rootJObj.getIntValue("code") == 1) {
                                JSONObject dataJObj = rootJObj.getJSONObject("data");
                                if (dataJObj.getIntValue("count") > 0) {
                                    List<SearchBean> goodsList = JSON.parseArray(
                                            dataJObj.getJSONArray("list").toJSONString(),
                                            SearchBean.class);
                                    EventBus.getDefault().post(new GotGoodsList(
                                            dataJObj.getIntValue("count"),
                                            dataJObj.getIntValue("total"),
                                            dataJObj.getIntValue("merchtype"), goodsList));
                                } else {// no more data
                                    EventBus.getDefault().post(new GotGoodsList(true));
                                }
                            } else {
                                EventBus.getDefault().post(new RequestResult(RequestResult.GOODS_LIST, Flag.Result.FAILURE));
                            }
                        }
                )
                .error((code, msg) -> {
                    EventBus.getDefault().post(new RequestResult(RequestResult.GOODS_LIST, Flag.Result.FAILURE));
                    LatteLogger.e("error", "requestGoodsList error code = "
                            + code + " msg = " + msg);
                })
                .failure(() -> {
                    EventBus.getDefault().post(new RequestResult(RequestResult.GOODS_LIST, Flag.Result.FAILURE));
                    LatteLogger.e("error", "requestGoodsList failure ");
                })
                .build()
                .post();
    }

    static void requestSubCategory(int cateId) {
        RestClient.builder()
                .url(API.CATE_CHILD_PAGE)
                .params("id", cateId)
                .success(response -> {
                    JSONObject dataObj = CommonUtil.parseDataFromRootJSON(response);
                    if (dataObj != null) {
                        JSONArray jList = dataObj.getJSONArray("list");
                        if (jList != null && jList.size() > 0) {
                            JSONObject jObj = jList.getJSONObject(0);
                            List<FilterBean> filterBeanList = JSON.parseArray(
                                    jObj.getJSONArray("subcate").toJSONString(),
                                    FilterBean.class);
                            EventBus.getDefault().post(new GotSubCate(Flag.Result.SUCCEED, filterBeanList));
                        } else {
                            EventBus.getDefault().post(new GotSubCate(Flag.Result.FAILURE, null));
                        }
                    } else {
                        EventBus.getDefault().post(new GotSubCate(Flag.Result.FAILURE, null));
                    }
                })
                .error((code, msg) -> {
                    EventBus.getDefault().post(new GotSubCate(Flag.Result.FAILURE, null));
                    LatteLogger.e("error", " requestSubCategory error code = " + code + " msg = " + msg);
                })
                .failure(() -> {
                    EventBus.getDefault().post(new GotSubCate(Flag.Result.FAILURE, null));
                    LatteLogger.e("error", " requestSubCategory failure ");
                })
                .build()
                .post();
    }

}
